from apps.education.courseStudent.baseViews import *
from apps.chat.message.functions import MessageFun

class myCourseViewSet(BaseCourseStudentViewSet):
    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = CourseStudentDao().findAll().filter(student__user=self.request.user)
        return _list
    
class CourseStudentViewSet(BaseCourseStudentViewSet):
    def get_queryset(self):
        params = getRequestParams(self.request)
        _list = CourseStudentDao().findAll()
        if params.get('course'):
            _list = _list.filter(student__checkStatus='PASS')
        return _list

    # 审核通过
    @action(methods=['post'], detail=False)
    def checkPassAction(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('obj_id')
        obj = CourseStudentDao().update(obj_id, {'checkStatus': 'PASS'})
        MessageFun().sendMessage(None, obj.student.user, 'system', {'title': '系统通知', 'content': '您报名的课程《'+obj.course.name+'》，已通过审核！'})
        return Response(response_success('操作成功'))
    
    # 审核拒绝
    @action(methods=['post'], detail=False)
    def checkRefuseAction(self, request):
        params = getRequestParams(self.request)
        obj_id = params.get('obj_id')
        obj = CourseStudentDao().update(obj_id, {'checkStatus': 'REFUSE', 'remark': params.get('remark')})
        MessageFun().sendMessage(None, obj.student.user, 'system', {'title': '系统通知', 'content': '您报名的课程《'+obj.course.name+'》，未通过！原因：' + obj.remark})
        return Response(response_success('操作成功'))